package com.healthtop.service.impl;

import com.healthtop.common.Specification;
import com.healthtop.dao.XsDictionaryDao;
import com.healthtop.dto.XsDictionaryDto;
import com.healthtop.model.XsDictionary;
import com.healthtop.model.XsDictionaryType;
import com.healthtop.service.XsDictionaryService;
import com.healthtop.service.XsDictionaryTypeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class XsDictionaryServiceImpl implements XsDictionaryService {

    @Autowired
    private XsDictionaryDao xsDictionaryDao;

    @Autowired
    private XsDictionaryTypeService xsDictionaryTypeService;

    @Override
    public XsDictionaryDto forward(XsDictionaryDto xsDictionaryDto) {
        List<XsDictionaryType> list = xsDictionaryTypeService.selectAll();
        if(xsDictionaryDto.getMode()== Specification.OperateMode.UPDATE.index){
            XsDictionary xsDictionary = xsDictionaryDao.getXsDictionaryById(xsDictionaryDto.getId());
            BeanUtils.copyProperties(xsDictionary,xsDictionaryDto);
        }
        xsDictionaryDto.setDictionaryType(list);
        return xsDictionaryDto;
    }

    @Override
    public void operate(XsDictionaryDto xsDictionaryDto) {
        XsDictionary xsDictionary = new XsDictionary();
        BeanUtils.copyProperties(xsDictionaryDto,xsDictionary);
        if(xsDictionaryDto.getType()==0){
            XsDictionaryType xsDictionaryType = new XsDictionaryType();
            xsDictionaryType.setName(xsDictionaryDto.getTypeName());
            xsDictionaryTypeService.insert(xsDictionaryType);
            xsDictionary.setType(xsDictionaryType.getId());
        }
        if(xsDictionaryDto.getMode()==Specification.OperateMode.CREATE.index){
            xsDictionary.setDelFalg("0");
            xsDictionaryDao.insert(xsDictionary);
        }else if(xsDictionaryDto.getMode()==Specification.OperateMode.UPDATE.index){
            xsDictionaryDao.update(xsDictionary);
        }
    }

    @Override
    public void delete(long id) {
        XsDictionary xsDictionary = new XsDictionary();
        xsDictionary.setId(id);
        xsDictionary.setDelFalg("1");
        xsDictionaryDao.delete(xsDictionary);
    }

    @Override
    public List<XsDictionary> selectByType(long type) {
        return xsDictionaryDao.selectByType(type);
    }
}
